package tools.scale
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class Scale9Grid
	{
		private var _bmd:BitmapData;
		private var _leftTop:Bitmap;
		private var _centerTop:Bitmap;
		private var _rightTop:Bitmap;
		private var _left:Bitmap;
		private var _center:Bitmap;
		private var _right:Bitmap;
		private var _leftBottom:Bitmap;
		private var _centerBottom:Bitmap;
		private var _rightBottom:Bitmap;
		private var _scaleTop:Number;
		private var _scaleBottom:Number;
		private var _scaleLeft:Number;
		private var _scaleRight:Number;
		private var _minWidth:Number;
		private var _minHeight:Number;
		
		public function Scale9Grid(source:DisplayObject,scaleline:ScaleLine)
		{
			_bmd = new BitmapData(source.width, source.height, true, 0x00000000);
			_bmd.draw(source);
			
			_scaleTop = scaleline.top;
			_scaleBottom = scaleline.bottom;
			_scaleLeft = scaleline.left;
			_scaleRight = scaleline.right;
			
			_minWidth = _scaleLeft + _scaleRight;
			_minHeight = _scaleTop + _scaleBottom;
			
			_leftTop = creatBitmap(0, 0, _scaleLeft, _scaleTop);
			
			_centerTop = creatBitmap(_scaleLeft, 0, _scaleRight - _scaleLeft, _scaleTop);
			
			_rightTop = creatBitmap(_scaleRight, 0, source.width - _scaleRight, _scaleTop);
			
			_left = creatBitmap(0, _scaleTop, _scaleLeft, _scaleBottom - _scaleTop);
			
			_center = creatBitmap(_scaleLeft, _scaleTop, _scaleRight - _scaleLeft, _scaleBottom - _scaleTop);
			
			_right = creatBitmap(_scaleRight, _scaleTop, source.width - _scaleRight, _scaleBottom - _scaleTop);
			
			_leftBottom = creatBitmap(0, _scaleBottom, _scaleLeft, source.height - _scaleBottom);
			
			_centerBottom = creatBitmap(_scaleLeft, _scaleBottom, _scaleRight - _scaleLeft, source.height - _scaleBottom);
			
			_rightBottom = creatBitmap(_scaleRight, _scaleBottom, source.width - _scaleRight, source.height - _scaleBottom);
		}
		
		private function creatBitmap(x:Number,y:Number,w:Number,h:Number):Bitmap
		{
			var bmd:BitmapData = new BitmapData(w,h);
			bmd.copyPixels(_bmd,new Rectangle(x,y,w,h),new Point(0,0));
			var bm:Bitmap = new Bitmap(bmd);
			bm.x = x;
			bm.y = y;
			return bm;
		}
		
		public function getSprite(w:Number,h:Number):Sprite
		{
			var sprite:Sprite = new Sprite();
			addInDisplayObject(sprite,w,h);
			return sprite;
		}
		
		public function getMovieClip(w:Number,h:Number):MovieClip
		{
			var movieclip:MovieClip = new MovieClip();
			addInDisplayObject(movieclip,w,h);
			return movieclip;
		}
		
		/*public function getShape(w:Number,h:Number):Shape
		{
			var args:Array = setSize(w,h);
			
		}*/
		
		private function addInDisplayObject(display:DisplayObjectContainer,w:Number,h:Number):void
		{
			var args:Array = setSize(w,h);
			for (var i:int = 0; i < args.length; i++) 
			{
				display.addChild(args[i]);
			}
		}
		
		private function setSize(w:Number,h:Number):Array
		{
			if(w < _minWidth)
			{
				w = _minWidth;
			}
			if(h < _minHeight)
			{
				h = _minHeight;
			}
			
			var wlength:Number = w - _leftTop.width - _rightTop.width;
			var hlength:Number = h - _leftTop.height - _leftBottom.height;
			
			_centerTop.width = wlength;
			_center.width = wlength;
			_centerBottom.width = wlength;
			_left.height = hlength;
			_center.height = hlength;
			_right.height = hlength;
			
			_rightTop.x = _leftTop.width + _centerTop.width;
			_right.x = _rightTop.x;
			_rightBottom.x = _rightTop.x;
			_leftBottom.y = _leftTop.height + _left.height;
			_centerBottom.y = _leftBottom.y;
			_rightBottom.y = _leftBottom.y;
			
			return [_leftTop,_centerTop,_rightTop,_left,_center,_right,_leftBottom,_centerBottom,_rightBottom];
		}
	}
}